package key;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Add
 */
@WebServlet("/Add")
public class Add extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	Log log = new Log();
	Database db = new Database();
	
	public void init(ServletConfig config) throws ServletException{
	}
     
    public Add() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uidS = (String) request.getSession().getAttribute("uid");
		String[] toAdd = request.getParameterValues("adt");
		String uname = (String) request.getSession().getAttribute("uname");
		
		int uid = Integer.parseInt(uidS);
		
		if(toAdd.length + db.getCount(uid) > db.getMax()){
			List<Info> timeList = db.createList(uname, uidS);
			request.setAttribute("uid", uidS);
			request.setAttribute("timeList", timeList);//返回结果集
			request.setAttribute("uname", uname);
			String inf="当前设定的时长上限为："+db.getMax()+"，请重新选择";
			request.setAttribute("inf", inf);
			request.getRequestDispatcher("inf.jsp").forward(request,response);
		} else {
		
		db.choose(toAdd, uid);
		
		List<Info> timeList = db.createList(uname, uidS);
		request.setAttribute("uid", uidS);
		request.setAttribute("timeList", timeList);//返回结果集
		request.setAttribute("uname", uname);
		request.getRequestDispatcher("inf.jsp").forward(request,response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
